﻿/**
 * 代码来源于LumaQQ.Net，在此感谢原作者
 * 本人在原代码基础上做了部分修改以适应本人的需要。
 * 请不要将该类库用于非法途径和商业途径，与此产生的一切法律后果与本人无关。
 * 有任何问题，可以访问我的博客：http://baiyafeng.com 留言
 * 使用本类库请遵循GPL v3协议。
 **/


using System;
using System.Collections.Generic;
using System.Text;

namespace SinaBlogQiangShafa
{
    public class RunTrigger : IRunable
    {
        private Job client;
        public RunTrigger(Job client,int timeSpan)
        {
            this.client = client;
            ThreadExcutor.RegisterIntervalObject(this, this, timeSpan, false);
        }
        #region IRunable Members

        /// <summary>
        /// 	<remark>abu 2008-03-11 </remark>
        /// </summary>
        /// <value></value>
        public bool IsRunning
        {
            get;
            private set;
        }

        /// <summary>
        /// 	<remark>abu 2008-03-07 </remark>
        /// </summary>
        /// <param name="state">The state.</param>
        /// <param name="timedOut">if set to <c>true</c> [timed out].</param>
        public void Run(object state, bool timedOut)
        {
            if (!IsRunning)
            {
                lock (this)
                {
                    if (!IsRunning)
                    {
                        IsRunning = true;
                        this.client.Run();

                        IsRunning = false; 
                    }
                }
            }

        }

        /// <summary>
        /// 	<remark>abu 2008-03-11 </remark>
        /// </summary>
        /// <value></value>
        public System.Threading.WaitHandle WaitHandler
        {
            get;
            set;
        }

        /// <summary>
        /// 	<remark>abu 2008-03-11 </remark>
        /// </summary>
        /// <value></value>
        public System.Threading.RegisteredWaitHandle RegisterdHandler
        {
            get;
            set;
        }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (this.WaitHandler != null && this.RegisterdHandler != null)
            {
                RegisterdHandler.Unregister(this.WaitHandler);
            }
        }

        #endregion
    }

}
